package com.example.testtouch2;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.widget.TextView;

import com.demo.androidgame.fx.Screen;
import com.demo.androidgame.fx.impl.GLGame;

public class MainScreen extends GLGame {

	boolean initialized = false;
	private NfcAdapter nfcAdapter;
	private PendingIntent pendingIntent;
	public static Tag tag;
	TextView tagDesc;

	 public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			
			nfcAdapter = NfcAdapter.getDefaultAdapter(this);
			Intent intent= new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
			
			}
	 
	@Override
	public void onPause() {

		super.onPause();
		if (nfcAdapter != null) {
			nfcAdapter.disableForegroundDispatch(this);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (nfcAdapter != null) {
			nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);			
		Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		byte[] tagId = tag.getId();
//		tagDesc.setText("tagid:" +toHexString(tagId));
		if (tag != null ) {
			//&& tagId.toString() == "044FD44A773480"
			WorldRenderer.nfcCheck = true;
			menuScreen.nfcCheck = true;
			World.nfcCheck = true;
			TestScreen.nfcCheck = true;
			HighscoresScreen.nfcCheck = true;
//			byte[] tagId = tag.getId();
//			String tagId2 = "0497E24A773480";
//			if((toHexString(tagId).equals(tagId2))){
//			
//			tagDesc.setText("tag recognition!!");
//			Log.d("tag");
			
			return;
		
		}
//		}
		else if(tag == null){
			WorldRenderer.nfcCheck = false;
			menuScreen.nfcCheck = false;
			World.nfcCheck = false;
			TestScreen.nfcCheck = false;
			HighscoresScreen.nfcCheck = false;
			return;
		}
		
	}
public static final String CHARS = "0123456789ABCDEF";
	
	public static String toHexString(byte[] data) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < data.length; ++i) {
			sb.append(CHARS.charAt((data[i] >> 4) & 0x0F))
				.append(CHARS.charAt(data[i] & 0x0F));
		}
		return sb.toString();
	}

	@Override
	public Screen getStartScreen() {
		SettingsColliderIO.load(getFileIO());
		Settings.load(getFileIO());
		return new convertScreen(this);
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		super.onSurfaceCreated(gl, config);
		if (initialized == false) {
			
			initialized = true;
			
		}
		Assets.load(this);
	}
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}
